package com.sfddata.musicpillow.user.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fasterxml.jackson.databind.ser.Serializers;
import com.netflix.ribbon.proxy.annotation.Http;
import com.sfddata.musicpillow.common.constant.BizCodeEnum;
import com.sfddata.musicpillow.common.respose.BaseRespose;
import com.sfddata.musicpillow.common.respose.EmptyObj;
import com.sfddata.musicpillow.user.entity.*;
import com.sfddata.musicpillow.user.exception.PasswordErrorException;
import com.sfddata.musicpillow.user.exception.UserNotExsitException;
import com.sfddata.musicpillow.user.exception.UserNotSetPasswordException;
import com.sfddata.musicpillow.user.service.*;
import com.sfddata.musicpillow.user.vo.CheckPassWord;
import com.sfddata.musicpillow.user.vo.UserInfoEntityVo;
import org.apache.http.HttpRequest;
import org.apache.http.params.HttpParams;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("user")
public class UserController {

    @Autowired
    UserInfoService userInfoService;
    @Autowired
    EquipmentInfoService equipmentInfoService;
    @Autowired
    UserEquipRelationService userEquipRelationService;
    @Autowired
    UserClockService userClockService;
    @Autowired
    SleepImprovePlanService sleepImprovePlanService;

    @PostMapping("/setPlan")
    public BaseRespose<EmptyObj> setPlan(@RequestBody SleepImprovePlanVo sleepImprovePlan, @RequestHeader String id) {

        BaseRespose<EmptyObj> respose = new BaseRespose<>();
        String scheduleId = null;
        try {
            scheduleId = sleepImprovePlanService.setPlan(sleepImprovePlan ,id);
        } catch (Exception e) {
            return respose
                    .error(BizCodeEnum.THIRD_SERVER_ERROR.getCode(), e.getMessage());
        }

        if (scheduleId != null){
            sleepImprovePlanService.savePlan(sleepImprovePlan, id, scheduleId);
        }
        respose.setCode(BizCodeEnum.SUCCESS.getCode());
        respose.setMsg(BizCodeEnum.SUCCESS.getMsg());
        return respose;
    }

    @GetMapping("/getPlan")
    public BaseRespose<List<SleepImprovePlanWithMathType>> getPlan(@RequestHeader String id) {

        BaseRespose<List<SleepImprovePlanWithMathType>> respose = new BaseRespose<>();

        List<SleepImprovePlanWithMathType> plans = sleepImprovePlanService.getByUserid(id);


        respose.setData(plans);
        respose.setCode(BizCodeEnum.SUCCESS.getCode());
        respose.setMsg(BizCodeEnum.SUCCESS.getMsg());
        return respose;
    }

    // 设置闹钟
    @PostMapping("/setClock")
    public BaseRespose<UserClock> setClock(@RequestBody UserClock clockInfo) {

        BaseRespose<UserClock> respose = new BaseRespose<>();
        UserClock userClock = userClockService.saveClock(clockInfo);

        respose.setData(userClock);
        respose.setCode(BizCodeEnum.SUCCESS.getCode());
        return respose;
    }


    // 获取闹钟
    @GetMapping("/getClock")
    public BaseRespose<UserClock> getClock(@RequestHeader String id) {
        BaseRespose<UserClock> respose = new BaseRespose<>();
        QueryWrapper<UserClock> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id" ,id);
        UserClock userClock = userClockService.getOne(
                queryWrapper
        );

        respose.setData(userClock);
        respose.setCode(BizCodeEnum.SUCCESS.getCode());
        return respose;
    }

    // 校验密码
    @PostMapping("/checkPassword")
    public BaseRespose<UserInfoEntity> checkPassword(@RequestBody CheckPassWord checkPassWord){

        //
        UserInfoEntity user = null;
        BaseRespose<UserInfoEntity> respose = new BaseRespose<UserInfoEntity>();
        try{
            user = userInfoService.checkPassword(checkPassWord);
            respose = respose.ok();
        }catch (UserNotExsitException une){
            respose = respose.error(
                    BizCodeEnum.ACCOUNT_NOTEXIST_ERROR.getCode(),
                    BizCodeEnum.ACCOUNT_NOTEXIST_ERROR.getMsg()
            );
        }catch (PasswordErrorException pee){
            respose = respose.error(
                    BizCodeEnum.LOGINACTT_PASSWORD_ERROR.getCode(),
                    BizCodeEnum.LOGINACTT_PASSWORD_ERROR.getMsg()
            );
        }catch (UserNotSetPasswordException pee){
            respose = respose.error(
                    BizCodeEnum.PASSWORD_NOT_SET.getCode(),
                    BizCodeEnum.PASSWORD_NOT_SET.getMsg()
            );
        }
        respose.setData(user);
        return respose;
    }

    // 修改用户信息
    @PostMapping("/modifyUserInformation")
    public BaseRespose<UserInfoEntityVo> updateUserInfo(@RequestBody() UserInfoEntityVo userInfoVo){

        BaseRespose<UserInfoEntityVo> respose = new BaseRespose<>();

        UserInfoEntity userInfo = userInfoService.updateUserInfo(userInfoVo);
        if (userInfo == null){
            // 如果是空证明account错了
            respose.setCode(BizCodeEnum.ACCOUNT_NOTEXIST_ERROR.getCode());
            respose.setMsg(BizCodeEnum.ACCOUNT_NOTEXIST_ERROR.getMsg());
            return respose;
        }
        UserInfoEntityVo vo = new UserInfoEntityVo();
        BeanUtils.copyProperties(userInfo, vo);
        vo.setRegisterTime();
        respose.setData(vo);
        respose.setCode(BizCodeEnum.SUCCESS.getCode());
         return respose;
    }

    // 通过手机号 获取用户信息
    @PostMapping("/getUserInfoByPhone")
    public BaseRespose<UserInfoEntityVo> getUserInfoByPhone(@RequestParam("phone") String phone){

        BaseRespose<UserInfoEntityVo> respose = new BaseRespose<>();
        UserInfoEntityVo vo = new UserInfoEntityVo();
        UserInfoEntity userInfo = userInfoService.insertIfDontExist(phone);
        if (userInfo.getGender() == null){
            respose.setMsg("用户信息为空");
            respose.setCode(BizCodeEnum.UNKNOW_EXCEPTION.getCode());
            return respose;
        }
        BeanUtils.copyProperties(userInfo, vo);
        vo.setRegisterTime();
        respose.setData(vo);
        respose.setCode(BizCodeEnum.SUCCESS.getCode());
        return respose;
    }

    // 获取用户信息
    @GetMapping("/getUserInfo")
    public BaseRespose<UserInfoEntityVo> getUserInfo(@RequestParam("userId") String userId){

        BaseRespose<UserInfoEntityVo> respose = new BaseRespose<>();
        UserInfoEntityVo vo = new UserInfoEntityVo();
        UserInfoEntity userInfo = userInfoService.getById(userId);
        if (userInfo == null){
            respose.setMsg("用户信息为空");
            respose.setCode(BizCodeEnum.UNKNOW_EXCEPTION.getCode());
            return respose;
        }
        BeanUtils.copyProperties(userInfo, vo);
        vo.setRegisterTime();
        respose.setData(vo);
        respose.setCode(BizCodeEnum.SUCCESS.getCode());
        return respose;
    }

    // 获取蓝牙信息
    @GetMapping("/getBleInfo")
    public BaseRespose<List<EquipmentInfoEntity>> getBleInfo(@RequestParam("userId") String userId){

        BaseRespose<List<EquipmentInfoEntity>> respose = new BaseRespose<>();
        List<EquipmentInfoEntity> equipList = userInfoService.getEquipList(userId);


        if(equipList != null){
            respose.setData(equipList);
            respose.setCode(BizCodeEnum.SUCCESS.getCode());
            return respose;
        }else {
            respose.setData(null);
            respose.setMsg("该用户没有绑定设备");
            respose.setCode(BizCodeEnum.SUCCESS.getCode());
            return respose;
        }
    }

    // 新增设备
    @PostMapping("/addEquip")
    public BaseRespose<EmptyObj> addEquip(@RequestBody() EquipWithPhone info){

        BaseRespose<EmptyObj> respose = new BaseRespose<>();

        try {
            equipmentInfoService.saveWithUser(info);
        }catch (Exception e){
            respose.setCode(BizCodeEnum.ACCOUNT_NOTEXIST_ERROR.getCode());
            respose.setMsg(e.getMessage());
            return respose;
        }
        respose.setCode(BizCodeEnum.SUCCESS.getCode());
        respose.setMsg(BizCodeEnum.SUCCESS.getMsg());
        return respose;
    }

    // 删除设备关联
    @PostMapping("/deleteEquipRelation")
    public BaseRespose<EmptyObj> deleteEquip (@RequestBody() EquipWithPhone info){
        BaseRespose<EmptyObj> respose = new BaseRespose<>();

        try {
            equipmentInfoService.removeRelation(info);
        }catch (Exception e){
            respose.setCode(BizCodeEnum.UNKNOW_EXCEPTION.getCode());
            respose.setMsg(e.getMessage());
            return respose;
        }
        respose.setCode(BizCodeEnum.SUCCESS.getCode());
        respose.setMsg(BizCodeEnum.SUCCESS.getMsg());
        return respose;
    }

    // 登出
    @PostMapping("/logoff")
    public BaseRespose<EmptyObj> logoff(@RequestParam("id") String id){

        BaseRespose<EmptyObj> respose = new BaseRespose<>();
        // fixme 还要删除关联的表里的
        boolean b = userInfoService.removeById(Integer.valueOf(id));
        if(b){
            respose.setCode(BizCodeEnum.SUCCESS.getCode());
            respose.setMsg(BizCodeEnum.SUCCESS.getMsg());
            return respose;
        }else {
            respose.setCode(BizCodeEnum.UNKNOW_EXCEPTION.getCode());
            respose.setMsg(BizCodeEnum.UNKNOW_EXCEPTION.getMsg());
            return respose;
        }
    }

    // 计算问卷得分
    @PostMapping("/questionnaire")
    public BaseRespose<EmptyObj> questionnaire(@RequestParam("baseList") List<String> baseList,
                                               @RequestParam("sleepList") List<String> sleepList,
                                               @RequestParam("psylist") List<String> psylist,
                                               @RequestParam("habitList") List<String> habitlist){

        BaseRespose<EmptyObj> respose = new BaseRespose<>();



        return respose;
    }
}
